/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.mnt.rest

import cn.hutool.db.PageResult

import io.swagger.v3.oas.annotations.media.Schema
import jakarta.servlet.http.HttpServletResponse
import me.zhengjie.logging.annotation.Log
import me.zhengjie.modules.mnt.domain.ServerDeploy
import me.zhengjie.modules.mnt.service.ServerDeployService
import me.zhengjie.modules.mnt.service.dto.ServerDeployDto
import me.zhengjie.modules.mnt.service.dto.ServerDeployQueryCriteria
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.io.IOException

/**
 * @author zhanghouying
 * @date 2019-08-24
 */
@RestController
@Schema(description = "运维：服务器管理")
@RequestMapping("/api/serverDeploy")
class ServerDeployController (
    private val serverDeployService: ServerDeployService? = null
){
    @Schema(description = "导出服务器数据")
    @GetMapping(value = ["/download"])
    @PreAuthorize("@el.check('serverDeploy:list')")
    @Throws(
        IOException::class
    )
    fun exportServerDeploy(response: HttpServletResponse?, criteria: ServerDeployQueryCriteria?) {
        serverDeployService!!.download(serverDeployService.queryAll(criteria), response)
    }

    @Schema(description = "查询服务器")
    @GetMapping
    @PreAuthorize("@el.check('serverDeploy:list')")
    fun queryServerDeploy(
        criteria: ServerDeployQueryCriteria?,
        pageable: Pageable?
    ): ResponseEntity<PageResult<ServerDeployDto>> {
        return ResponseEntity<PageResult<ServerDeployDto>>(
            serverDeployService!!.queryAll(criteria, pageable) as PageResult<ServerDeployDto>,
            HttpStatus.OK
        )
    }

    @Log("新增服务器")
    @Schema(description = "新增服务器")
    @PostMapping
    @PreAuthorize("@el.check('serverDeploy:add')")
    fun createServerDeploy(@Validated @RequestBody resources: ServerDeploy?): ResponseEntity<Any> {
        serverDeployService!!.create(resources)
        return ResponseEntity<Any>(HttpStatus.CREATED)
    }

    @Log("修改服务器")
    @Schema(description = "修改服务器")
    @PutMapping
    @PreAuthorize("@el.check('serverDeploy:edit')")
    fun updateServerDeploy(@Validated @RequestBody resources: ServerDeploy?): ResponseEntity<Any> {
        serverDeployService!!.update(resources!!)
        return ResponseEntity<Any>(HttpStatus.NO_CONTENT)
    }

    @Log("删除服务器")
    @Schema(description = "删除Server")
    @DeleteMapping
    @PreAuthorize("@el.check('serverDeploy:del')")
    fun deleteServerDeploy(@RequestBody ids: Set<Long?>?): ResponseEntity<Any> {
        serverDeployService!!.delete(ids!!)
        return ResponseEntity<Any>(HttpStatus.OK)
    }

    @Log("测试连接服务器")
    @Schema(description = "测试连接服务器")
    @PostMapping("/testConnect")
    @PreAuthorize("@el.check('serverDeploy:add')")
    fun testConnectServerDeploy(@Validated @RequestBody resources: ServerDeploy?): ResponseEntity<Any> {
        return ResponseEntity<Any>(serverDeployService!!.testConnect(resources!!), HttpStatus.CREATED)
    }
}